import Dict from './Dict'
import { mergeOptions } from './DictOptions'

export default function(Vue, options) {
  mergeOptions(options)  //这个地方最先调用了mergeOptions，将参数options和mergeOptions的options合并了。
  Vue.mixin({
    data() {
      //this.$options 获取vue组件的方法和数据
      // console.log('this.$options:',this.$options)
      if (this.$options === undefined || this.$options.dicts === undefined || this.$options.dicts === null) {
        return {}
      }
      const dict = new Dict()
      dict.owner = this   
      return {
        dict
      }  
    },

    created() {
      if (!(this.dict instanceof Dict)) {
        return
      }
      // debugger
      options.onCreated && options.onCreated(this.dict)  // 这里的options 是函数接收到的参数的options。打印出来，options.onCreated返回的是undefined，全局都没有地方有onCreated，是不是又可能vue的其他组件会使用这个方法。
      this.dict.init(this.$options.dicts).then(() => {
        options.onReady && options.onReady(this.dict)
        this.$nextTick(() => {
          this.$emit('dictReady', this.dict)
          if (this.$options.methods && this.$options.methods.onDictReady instanceof Function) {
            this.$options.methods.onDictReady.call(this, this.dict)
          }
        })
      })
    },
  })
}
